package com.relife.core.easyui;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.alibaba.fastjson.JSONObject;
import com.relife.common.utils.StringUtil;

public class UploadTag extends TagSupport {

	private static final long serialVersionUID = 2923092931512063187L;
	private String formData;
	private String name = "上传文件";
	private String onUploadSuccess;
	private String fileTypeExts = "*.gif;*.jpg;*.png;*.jpeg;*.bmp;";
	private String fileName = "file";
	private String dir = "image";
	private Integer uploadLimit = 99;
	private String queueID = "upload";
	private Integer height = 25;
	private Integer width = 100;
	private String url;
	private String fileMaxSize = "5MB";
	private String swf;

	@Override
	public int doStartTag() throws JspTagException {
		if (StringUtil.isEmpty(swf)) {
			HttpServletRequest req = (HttpServletRequest) this.pageContext.getRequest();
			swf = req.getContextPath() + "/common/uploadify/uploadify.swf";
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			out.print(this.getHtml());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private String getHtml() {
		StringBuffer sb = new StringBuffer("");
		sb.append("\n<input id=" + fileName + " type=\"file\">\n");
		sb.append("<script type=\"text/javascript\">\n");
		sb.append("var files=0;\n");
		sb.append("$(\"#" + fileName + "\").uploadify({\n");
		sb.append(" buttonText   :'" + name + "',\n");
		sb.append(" fileObjName  :'" + fileName + "' ,\n");
		sb.append(" fileTypeExts : '" + fileTypeExts + "',\n");
		sb.append(" fileSizeLimit: '" + fileMaxSize + "',\n");
		sb.append(" queueID      : '" + queueID + "',\n");
		sb.append(" height       : " + height + ",\n");
		sb.append(" width        : " + width + ",\n");
		sb.append(" uploadLimit  : " + uploadLimit + ",\n");
		sb.append(" swf          : '" + swf + "',\n");
		sb.append(" uploader     : '" + getUrl() + "',\n");
		sb.append(" formData     : " + getFormData() + ",\n");
		sb.append(" onInit       : function(instance) {\n");
		sb.append(" var html ='<table style=\"width: 100%;height: 100%\"><tr><td valign=\"middle\" align=\"center\" id=\"upload\"></td></tr></table>';\n");
		sb.append(" $(\"<div id='fileUpload' class='fileUpload'>\" + html + \"</div>\").appendTo(\"body\");\n");
		sb.append(" },\n");
		sb.append(" onDialogClose : function(queueData) {\n");
		sb.append(" files+=queueData.filesSelected;\n");
		sb.append(" if(queueData.filesSelected){\n");
		sb.append("   $(\"#fileUpload\").show();\n");
		sb.append("   $(\"form\").attr(\"lock\", \"lock\");\n");
		sb.append(" }\n");
		sb.append("},\n");
		sb.append(" onUploadSuccess  : function(file, data, response){\n");
		sb.append("	var obj = $.parseJSON(data);\n");
		sb.append("	" + onUploadSuccess + "(obj);\n");
		sb.append(" if(files==(file.index+1)){\n");
		sb.append("    $(\"#fileUpload\").hide();\n");
		sb.append("    $(\"form\").attr(\"lock\", \"\");\n");
		sb.append(" }\n");
		sb.append("}\n");
		sb.append("});\n");
		sb.append("</script>\n");
		return sb.toString();
	}

	public String getFormData() {
		if (StringUtil.isEmpty(formData)) {
			return "{'dir':'" + dir + "'}";
		} else {
			JSONObject obj = JSONObject.parseObject(formData);
			obj.put("dir", dir);
			return obj.toString();
		}
	}

	public void setFormData(String formData) {
		this.formData = formData;
	}

	public String getOnUploadSuccess() {
		return onUploadSuccess;
	}

	public void setOnUploadSuccess(String onUploadSuccess) {
		this.onUploadSuccess = onUploadSuccess;
	}

	public String getFileTypeExts() {
		return fileTypeExts;
	}

	public void setFileTypeExts(String fileTypeExts) {
		this.fileTypeExts = fileTypeExts;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public Integer getUploadLimit() {
		return uploadLimit;
	}

	public void setUploadLimit(Integer uploadLimit) {
		this.uploadLimit = uploadLimit;
	}

	public String getQueueID() {
		return queueID;
	}

	public void setQueueID(String queueID) {
		this.queueID = queueID;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public String getUrl() {
		if (StringUtil.isEmpty(url)) {
			HttpServletRequest req = (HttpServletRequest) this.pageContext.getRequest();
			url = req.getContextPath() + "/sys/file/uploadFile.do";
		}
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
